/*
 * #%L
 * Client
 * %%
 * Copyright (C) 2011 Ractoc.com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.ractoc.opengamefinder.client.plugins.nifty;

import com.ractoc.opengamefinder.client.screens.controllers.MessageController;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.ControlBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.PopupBuilder;
import de.lessvoid.nifty.elements.Element;

/**
 * Creates a mesage box. This is a modal box so it restricts access to the underlying screen.
 * @author ractoc
 * @since 0.1
 */
public class MessageBox {

    private static Element popup;

    /**
     * Creates and displays the message box.
     * @param nifty Nifty instance to create the message box with.
     * @param text The text inside the message box.
     * @param icon The icon to display in the message box.
     */
    public static void showMessageBox(Nifty nifty, String text, String icon) {
        if (popup == null) {
            new PopupBuilder("consolePopup") {
                
                {
                    childLayoutAbsolute();
                    backgroundColor("#f00f");
                    set("controller", "com.ractoc.opengamefinder.client.screens.controllers.MessageController");  
                    
                    panel(new PanelBuilder() {

                        {
                            childLayoutCenter();
                            width("100%");
                            height("100%");
                            alignCenter();
                            valignCenter();
                            control(new ControlBuilder("messagePanel") {

                                {
                                    width("80%");
                                    alignCenter();
                                    valignCenter();
                                }
                            });
                        }
                    });
                }
            }.registerPopup(nifty);
            popup = nifty.createPopup("consolePopup");
        }
        popup.getControl(MessageController.class).showMessage(text, icon);
        nifty.showPopup(nifty.getCurrentScreen(), popup.getId(), null);
    }
}
